<?php

    class entries
    {
        private $mikron;
        public function __construct(&$mikron)
        {
            $this->mikron = $mikron;
            /**
            * Добавляем приложение для возможности вызова его методов через URI вида
            * /action/entries/delete_entry/?id=31028079-d19c-4fd6-8722-04f34da89a76
            */
            $mikron->Applications->Add($this);
            /**
            * Инструкция вида
            * {{WEBDOCTOR_ENTRIES}} выведет весь список записей
            */
            $this->mikron->OperatingInstructions->AddFunction('WEBDOCTOR_ENTRIES', 'DrawEntries', $this);
        }
        public function delete_entry($parameters)
        {
            $id = $this->mikron->Tools->GetParam($parameters, 'id', null);
            $ret = $this->mikron->Tools->GetParam($parameters, 'ret', null);
            $remote_lpu_id = $this->mikron->Tools->GetParam($parameters, 'lpu', null);
            if(is_null($id) || is_null($ret) || is_null($remote_lpu_id))
            {
                $this->mikron->Tools->ThrowPage('Ошибка', 403, '/');
                return false;
            }
            $delete_do = new DataObject('delete_entry');
            $delete_do->SetGuid('entryid', $id);
            $result = $this->mikron->Site->Soap->Call($remote_lpu_id, $delete_do);
            $this->mikron->Tools->GoToPage($ret);
        }
        public function DrawEntries()
        {
            global $mikron;
            $request_uri = $_SERVER['REQUEST_URI'];
            $loginInfoId = $this->mikron->Users->CurrentUser()->user->logininfoid->value;
            $get_entries = new DataObject('get_entries');
            $get_entries->SetGuid('logininfoid', $loginInfoId);
            try
            {
                $entries = @$this->mikron->Site->Soap->Call(null, $get_entries);
            }
            catch(Exception $ex)
            {
                echo 'Не удалось получить список.';
                return;
            }
            if($entries instanceof MikronException || $entries->name != 'get_entries')
            {
                $this->mikron->Site->ShowException(new MikronException('Ошибка сервера обработки команды запроса списка записей.'));
                
                return;
            }
            try
            {
                $table = new html_table(2, 'table_nice', null, 0, 4);
                if($entries instanceof MikronException)
                {
                    return $this->mikron->Site->ShowException($entries);
                }
                $persons_count = 0;
                foreach($entries->GetList('persons') as $person)
                {
                    foreach($person->GetList('ambulance_cards') as $ambulance_card)
                    {
                        $ac_id = $ambulance_card->GetGuid('ac_id');
                        $cf_id = $ambulance_card->GetGuid('card_file_id');
                        $cardfile_fio = $ambulance_card->GetString('card_file_fio');
                        $cardfile_birthday = $ambulance_card->GetOADateTimeMSSQL('card_file_birthday');
                        $cardfile_birthday = $this->mikron->Tools->DateTime->BeautifulDate($cardfile_birthday);
                        $person_tostring = sprintf('%s <small>%sг.р.</small>', $cardfile_fio, $cardfile_birthday);
                        $remote_lpu_id = $ambulance_card->GetGuid('remote_lpu_id');
                        foreach($person->GetList('ambulance_cards') as $ambulance_card)
                        {
                            $entry = new DataObject(123);
                            foreach($ambulance_card->GetList('entries') as $entry)
                            {
                                $entry_id = $entry->GetGuid('entry_id');
                                $time_or_turn = $entry->GetString('time_or_turn');
                                $work_time = $entry->GetString('work_time');
                                $entry_date = $entry->GetOADateTimeMSSQL('entry_date');
                                $entry_date = date($this->mikron->Constants->DateFormatToString, $entry_date);
                                $entry_number = $entry->GetString('number');
                                $entry_place = $entry->GetString('place');
                                if($entry_place == ''){$entry_place='<small>не указан</small>';}
                                $spec_fio = $entry->GetString('worker_fio');
                                $spec = $entry->GetString('speciality_name');
                                $spec_tostring = sprintf('%s <small>%s</small>', $spec, $spec_fio);
                                $string = "<h3>Запись №$entry_number</h3>
                                Кто: $person_tostring<br />
                                К кому: $spec_tostring<br />
                                Режим работы специалиста: $work_time<br />
                                Дата приема: $entry_date<br />
                                Время приема: $time_or_turn<br />
                                Кабинет: $entry_place
                                <br /><a onclick=\"return confirm('Вы действительно хотите удалить запись на прием?');\"
                                title=\"Удаление записи на прием\"
                                href=\"/action/entries/delete_entry/?id=$entry_id&ret=$request_uri&lpu=$remote_lpu_id\">Удалить запись</a>";
                                $table->newcol($string);
                                $persons_count++;
                            }
                        }
                    }
                }
                $table->end();
                if($persons_count == 0)
                {
                    echo 'Список пуст.';
                }
            }
            catch(Exception $ex)
            {
                echo $ex->getMessage();
            }
        }
    }

?>